/*
 This file 'Get' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2014-06-19 16:17
 */

define(function (require, exports, module) {

	var AjaxFilter = require("Firebird.filter.AjaxFilter");
	var AjaxException = require("Firebird.lang.AjaxException");

	require("Firebird.Model");

	/**
	 * Firebird.ajax.Get
	 * @class Firebird.ajax.Get
	 */
	Firebird.define("Firebird.ajax.Get", {
		extend: "Firebird.Model",
		config: {
			beforeSend: null,
			afterSend: null
		},
		/**
		 * 静态属性
		 */
		statics: {},
		_init: function () {

			var options = this.options = $.extend({}, {
				type: "get",
				dataType: "json",
				timeout: 20000,
				headers: {
					"X-Requested-Author": "FirebirdAjaxRequest",
					"X-Requested-Firebird-Ajax-Type": "GET"
				},
				url: "",
				cache: false,
				beforeSend: this.beforeSend || Firebird.emptyFn,
				complete: function () {
					firebird.info(arguments);
				},
				success: function () {
					firebird.info(arguments);
				},
				error: function (jqXHR, textStatus, errorThrown) {
					firebird.error(arguments);
				},
				statusCode: {
					400: function () {
						doErrorHandler("sysError", 400);

					},
					401: function () {
						doErrorHandler("sysError", 401);
					},
					402: function () {
						doErrorHandler("sysError", 402);
					},
					403: function () {
						doErrorHandler("sysError", 403);
					},
					404: function () {
						doErrorHandler("sysError", 404);
					},
					405: function () {
						doErrorHandler("sysError", 405);
					},
					406: function () {
						doErrorHandler("sysError", 406);
					},
					407: function () {
						doErrorHandler("sysError", 407);
					},
					408: function () {
						doErrorHandler("sysError", 408);
					},
					409: function () {
						doErrorHandler("sysError", 409);
					},
					410: function () {
						doErrorHandler("sysError", 410);
					},
					411: function () {
						doErrorHandler("sysError", 411);
					},
					412: function () {
						doErrorHandler("sysError", 412);
					},
					414: function () {
						doErrorHandler("sysError", 414);
					},
					417: function () {
						doErrorHandler("sysError", 417);
					},
					421: function () {
						doErrorHandler("sysError", 421);
					},
					422: function () {
						doErrorHandler("sysError", 422);
					},
					423: function () {
						doErrorHandler("sysError", 423);
					},
					424: function () {
						doErrorHandler("sysError", 424);
					},
					425: function () {
						doErrorHandler("sysError", 425);
					},
					426: function () {
						doErrorHandler("sysError", 426);
					},
					449: function () {
						doErrorHandler("sysError", 449);
					},
					500: function () {
						doErrorHandler("sysError", 500);
					},
					501: function () {
						doErrorHandler("sysError", 501);
					},
					502: function () {
						doErrorHandler("sysError", 502);
					},
					503: function () {
						doErrorHandler("sysError", 503);
					},
					504: function () {
						doErrorHandler("sysError", 504);
					},
					507: function () {
						doErrorHandler("sysError", 507);
					},
					508: function () {
						doErrorHandler("sysError", 508);
					},
					510: function () {
						doErrorHandler("sysError", 510);
					}
				}
			}, this.initialConfig);

			firebird.info("Firebird.ajax.Get::init ", options.url);

			var success = options.success;
			var error = options.error;
			var complete = options.complete;

			options.success = function (text, statusText, jqXHR) {

				new AjaxFilter();

				if (options.dataType == "json" && (text.errCode || text.errMsg || text.errStack)) {
					doErrorHandler("bizError", text);
				}

				success && success.apply(this, [text, statusText, jqXHR]);

			};

			options.error = function (text, statusText, e) {

				if (Firebird.isString(e)) {

					switch (e) {
						case "Not found":
							break;
					}

				} else {
					doErrorHandler("bizError", statusText, e);
				}

				error && error.apply(this, [text, statusText, e]);
			};

			options.complete = function (text, statusText, jqXHR) {
				complete && complete();
			};

			var sysErrStack = [];

			firebird.api.mix("languagePack", {
				zh_CN: {
					"firebird.ajax.error.400": "请求参数错误 400",
					"firebird.ajax.error.403": "没有权限访问相应资源 403",
					"firebird.ajax.error.404": "请求的内容未找到 404",
					"firebird.ajax.error.406": "请求的内容未找到 406",
					"firebird.ajax.error.500": "服务器异常 500",
					"firebird.ajax.error.502": "服务器异常 502",
					"firebird.ajax.error.503": "服务器异常 503",
					"firebird.ajax.error.parsererror": "解析异常"
				},
				en_US: {
					"firebird.ajax.error.400": "请求参数错误 400",
					"firebird.ajax.error.403": "没有权限访问相应资源 403",
					"firebird.ajax.error.404": "请求的内容未找到 404",
					"firebird.ajax.error.406": "请求的内容未找到 406",
					"firebird.ajax.error.500": "服务器异常 500",
					"firebird.ajax.error.502": "服务器异常 502",
					"firebird.ajax.error.503": "服务器异常 503",
					"firebird.ajax.error.parsererror": "解析异常"
				}
			});

			var parseCode = function (code) {
				return firebird.getText("firebird.ajax.error." + code);
			};

			var doErrorHandler = function (type, code, e) {

				sysErrStack.push(parseCode(code));
				var json = code;

				Firebird.create("Firebird.lang.AjaxException", {
					code: json.errorCode || parseCode(code),
					message: json.errMsg || sysErrStack.join("\n"),
					stack: fb.devMode === false ? json.errMsg : (sysErrStack.join("\n") + (e && e.stack || "\n\n") + (function () {
						var err = new Error();
						var stack = err.stack;
						if (Firebird.isString(json.errStack)) {
							stack = json.errStack;
						}
						if (Firebird.isArray(json.errStack)) {
							stack = json.errStack.join("\n\n");
						}
						return stack;
					})())
				});

				firebird.error("Firebird.ajax.Get::error", xhr);
			};

			var xhr = this.xhr = $.ajax(options);

		}
	});

	module.exports = Firebird.ajax.Get;

});